import rollup from 'rollup'
import typescript from '@rollup/plugin-typescript';
import commonjs from '@rollup/plugin-commonjs';
import { babel } from '@rollup/plugin-babel';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import del from 'rollup-plugin-delete'
import serve from 'rollup-plugin-serve'
import copyfiles from 'copyfiles'

const isDev = process.env.NODE_ENV == 'dev'

const conf = {
    input: 'src/index.ts',
    output: [
        {
            format: 'umd',
            name: 'AmapUtils',
            file: 'dist/index.umd.js'
        },
        {
            format: 'es',
            file: 'dist/index.es.js'
        },
        {
            format: 'cjs',
            file: 'dist/index.cjs.js'
        }
    ],

    plugins: [
        del({ targets: 'dist/*' }),
        nodeResolve(),
        typescript(),
        commonjs(),
        babel({ babelHelpers: 'bundled' }),
        isDev && serve({
            // Multiple folders to serve from
            contentBase: ['dist', 'examples/html'],
            // Options used in setting up server
            host: '0.0.0.0',
            port: 10001,
        })
    ]
};

const watcher = rollup.watch(conf);
watcher.on('event', event => {
    switch (event.code) {
        case 'START':
            console.info('Rebuilding...');

            break;
        case 'BUNDLE_START':
            console.info('Bundling...');
            break;
        case 'BUNDLE_END':
            console.info('Bundled!');
            break;
        case 'END':
            console.info('Done!');
            copyfiles(['./dist/*.js', './dist/*.ts', './examples/html'], { all: true }, function () { })
            break;
        case 'ERROR':
        case 'FATAL':
            console.error("Rollup error: ", event);
    }
});

export default conf